package ch.ethz.fcl.metrobuzz.controller.gui.twl;

import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.util.ArrayList;

import org.lwjgl.opengl.Display;

import ch.ethz.fcl.metrobuzz.controller.MBController;
import ch.ethz.fcl.metrobuzz.viewer.MBViewer;
import de.matthiasmann.twl.BoxLayout;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.DesktopArea;
import de.matthiasmann.twl.GUI;
import de.matthiasmann.twl.Label;
import de.matthiasmann.twl.ToggleButton;
import de.matthiasmann.twl.Widget;

public class RootPane extends Widget {
	private MBController control;

	private DesktopArea desk;

	private BoxLayout timeBox;
	private BoxLayout helpBox;
	private BoxLayout setBox;
	private BoxLayout fullScreenBox;
	private BoxLayout titleBox;

	private ArrayList<Widget> widgets;

	private Widget timeFrame;
	// private Widget helpFrame;

	private ToggleButton fullscreen;

	private int top, bottom;

	private static boolean FULLSCREEN = false;

	public RootPane(MBController control) {
		this.control = control;
		setTheme("");

		init();

		timeBox = new BoxLayout(BoxLayout.Direction.HORIZONTAL);
		timeBox.setTheme("buttonBox");

		helpBox = new BoxLayout(BoxLayout.Direction.HORIZONTAL);
		helpBox.setTheme("buttonBox");

		setBox = new BoxLayout(BoxLayout.Direction.HORIZONTAL);
		setBox.setTheme("buttonBox");

		fullScreenBox = new BoxLayout(BoxLayout.Direction.HORIZONTAL);
		fullScreenBox.setTheme("buttonBox");

		titleBox = new BoxLayout(BoxLayout.Direction.HORIZONTAL);
		titleBox.setTheme("buttonBox");

		if (control.getContainer() != null) {
			Button reset = new Button("Reset");
			reset.addCallback(new ButtonCallBack(ButtonName.Reset, this));
			setBox.add(reset);
			widgets.add(reset);
		}

		Button exit = new Button("Exit");
		exit.addCallback(new ButtonCallBack(ButtonName.Exit, this));
		setBox.add(exit);
		widgets.add(exit);

		fullscreen = new ToggleButton();
		fullscreen.addCallback(new ButtonCallBack(ButtonName.FullScreen, this));
		fullscreen.setTheme("/checkbox");
		fullscreen.setActive(FULLSCREEN);

		Label fsLabel = new Label("Full Screen");
		fsLabel.setTheme("labelblue");
		fsLabel.setLabelFor(fullscreen);

		fullScreenBox.add(fsLabel);
		fullScreenBox.add(fullscreen);

		widgets.add(fullscreen);
		widgets.add(fsLabel);

		Label title = new Label("Singapore MRT Interchange Visualization");
		title.setTheme("labelblue");
		titleBox.add(title);
		widgets.add(title);

		add(desk);
		add(timeBox);
		// add(setBox);
		add(helpBox);
		// add(fullScreenBox);
		// add(titleBox);
	}

	public ArrayList<Widget> getWidgets() {
		return widgets;
	}

	public void init() {
		desk = new DesktopArea();
		desk.setTheme("");
		widgets = new ArrayList<Widget>();
	}

	public void addFrame(Widget widget) {
		desk.add(widget);
		widgets.add(widget);
	}

	public Button addButton(String text, Runnable cb, Widget widget) {
		ToggleButton btn = new ToggleButton(text);
		btn.addCallback(cb);
		if (text.equals("Help")) {
			helpBox.add(btn);
			// helpFrame = widget;
		} else if (text.equals("Time")) {
			timeBox.add(btn);
			timeFrame = widget;
		}
		widgets.add(btn);
		invalidateLayout();
		btn.setActive(true);
		widget.setVisible(false);
		return btn;
	}

	private void setButtonPosition() {
		fullScreenBox.setPosition(0, bottom - fullScreenBox.getHeight());
		titleBox.setPosition(Display.getWidth() / 2 - titleBox.getWidth() / 2,
				bottom - titleBox.getHeight());
		setBox.setPosition(Display.getWidth() - setBox.getWidth(), bottom
				- setBox.getHeight());

		helpBox.setPosition(Display.getWidth() - helpBox.getWidth(), top);
		timeBox.setPosition(0, top);
	}

	@Override
	public void layout() {
		top = 0;
		bottom = getHeight();

		String osName = System.getProperty("os.name").toLowerCase();

		if (osName.contains("mac")) {
			top = (int) (getParent().getHeight() + MBViewer.getBounds().y - MBViewer
					.getBounds().getHeight());
			GraphicsEnvironment env = GraphicsEnvironment
					.getLocalGraphicsEnvironment();
			Rectangle bounds = env.getMaximumWindowBounds();
			bottom = (int) (bounds.getHeight()
					- MBViewer.getBounds().getHeight() + getGUI().getRenderer()
					.getHeight());
		} else if (osName.contains("win")) {
			top = 0;
			bottom = getGUI().getRenderer().getHeight();
		}

		if (FULLSCREEN) {
			top = 0;
			bottom = Display.getDesktopDisplayMode().getHeight();
		}

		setBox.adjustSize();
		fullScreenBox.adjustSize();
		titleBox.adjustSize();

		helpBox.adjustSize();
		timeBox.adjustSize();

		desk.setPosition(0, top);
		desk.setSize(MBViewer.getWidth(), MBViewer.getHeight());

		timeFrame.adjustSize();
		timeFrame.setPosition(0, timeBox.getHeight());

		// helpFrame.adjustSize();
		// helpFrame.setPosition(getParent().getWidth() - helpFrame.getWidth(),
		// helpBox.getHeight());

		setButtonPosition();
	}

	@Override
	protected void afterAddToGUI(GUI gui) {
		super.afterAddToGUI(gui);
		validateLayout();
	}

	private class ButtonCallBack implements Runnable {
		private ButtonName name;
		private RootPane root;

		public ButtonCallBack(ButtonName name, RootPane root) {
			this.name = name;
			this.root = root;
		}

		@Override
		public void run() {
			if (name == ButtonName.Reset)
				control.getContainer().reset();
			else if (name == ButtonName.Exit) {
				Display.destroy();
				System.exit(0);
			} else if (name == ButtonName.FullScreen) {
				FULLSCREEN = root.fullscreen.isActive();

				control.getTWLGUI().getGUI().destroy();
				control.getTWLGUI().getRenderer().getActiveCacheContext()
						.destroy();
				Display.destroy();

				control.getContainer().createFont();

				control.resetTWLGUI();

				root.setButtonPosition();
			}
		}
	}

	private enum ButtonName {
		Reset, Exit, FullScreen;
	}
}
